﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.Controls;
using Newtonsoft.Json;
using DAL;
using Model;
using Newtonsoft.Json.Linq;

namespace FubiManger.UI
{
    partial class EditTimeForm : Smobiler.Core.Controls.MobileForm
    {
        public string merchantid { get; set; }
        Merchnathours hours = new Merchnathours();
        public EditTimeForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        bool flag0 = false;
        /// <summary>
        /// 星期日
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn0_Press(object sender, EventArgs e)
        {
            if (flag0 == false)
            {
                btn0.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag0 = true;
            }
            else
            {
                btn0.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag0 = false;
            }
        }
        bool flag1 = false;
        /// <summary>
        /// 星期一
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn1_Press(object sender, EventArgs e)
        {
            if (flag1 == false)
            {
                btn1.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag1 = true;
            }
            else
            {
                btn1.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag1 = false;
            }
        }
        bool flag2 = false;
        /// <summary>
        /// 星期二
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn2_Press(object sender, EventArgs e)
        {
            if (flag2 == false)
            {
                btn2.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag2 = true;
            }
            else
            {
                btn2.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag2 = false;
            }
        }
        bool flag3 = false;
        /// <summary>
        /// 星期三
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn3_Press(object sender, EventArgs e)
        {
            if (flag3 == false)
            {
                btn3.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag3 = true;
            }
            else
            {
                btn3.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag3 = false;
            }
        }
        bool flag4 = false;
        /// <summary>
        /// 星期四
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn4_Press(object sender, EventArgs e)
        {
            if (flag4 == false)
            {
                btn4.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag4 = true;
            }
            else
            {
                btn4.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag4 = false;
            }
        }
        bool flag5 = false;
        /// <summary>
        /// 星期五
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn5_Press(object sender, EventArgs e)
        {
            if (flag5 == false)
            {
                btn5.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag5 = true;
            }
            else
            {
                btn5.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag5 = false;
            }
        }
        bool flag6 = false;
        /// <summary>
        /// 星期六
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn6_Press(object sender, EventArgs e)
        {
            if (flag6 == false)
            {
                btn6.BackColor = System.Drawing.Color.FromArgb(255, 194, 193);
                flag6 = true;
            }
            else
            {
                btn6.BackColor = System.Drawing.Color.FromArgb(238, 238, 238);
                flag6 = false;
            }
        }

        public class Merchnathours
        {
            /// <summary>
            /// 营业周天
            /// </summary>
            public Array week { get; set; }
            /// <summary>
            /// 营业时间
            /// </summary>
            public Array hour { get; set; }
        }

        private void btnupdate_Press(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txthour.Text) == 0)
            {
                MessageBox.Show("请设置营业时间开始时间");
            }
            else if (Convert.ToInt32(txthour1.Text) == 0)
            {
                MessageBox.Show("请设置营业时间结束时间");
            }
            else if (string.IsNullOrEmpty(startweek) || string.IsNullOrEmpty(endweek))
            {
                MessageBox.Show("请设置营业天数");
            }
            else
            {
                string hour = txthour.Text + ":" + txtminiute.Text + "," + txthour1.Text + ":" + txtminiute1.Text;
                string week = startweek + "," + endweek;
                hours.hour = hour.Split(',');
                hours.week = week.Split(',');
                string json = JsonConvert.SerializeObject(hours);//积分商品信息
                if (merchantDal.Updatebusiness_hours(json, merchantid) > 0)
                {
                    Toast("更新成功");
                }

            }

        }
        string startweek = "";
        string endweek = "";
        private void pandays_Press(object sender, EventArgs e)
        {
            dayscontrol con = new dayscontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true && con.flag1 == true)
                {
                    startweek = con.startweek;
                    endweek = con.endweek;
                    lblweek.Text = con.startweek + "至" + con.endweek;
                }
            });
        }

        private void EditTimeForm_Load(object sender, EventArgs e)
        {

            ms_merchant merchant = merchantDal.Querybyid(merchantid);
            if (!string.IsNullOrEmpty(merchant.business_hours))
            {
                try
                {
                    JObject json = (JObject)JsonConvert.DeserializeObject(merchant.business_hours);
                    Array arr1 = json["week"].ToArray();
                    Array arr2 = json["hour"].ToArray();

                    DateTime dt1 = Convert.ToDateTime(arr2.GetValue(0));
                    DateTime dt2 = Convert.ToDateTime(arr2.GetValue(1));
                    txthour.Text = dt1.Hour.ToString("00");
                    txtminiute.Text = dt1.Minute.ToString("00");
                    txthour1.Text = dt2.Hour.ToString("00");
                    txtminiute1.Text = dt2.Minute.ToString("00");
                    lblweek.Text = arr1.GetValue(0) + "至" + arr1.GetValue(1);
                }
                catch (Exception)
                {

                    txthour.Text = "00";
                    txtminiute.Text = "00";
                    txthour1.Text = "00";
                    txtminiute1.Text = "00";
                    lblweek.Text = "";
                }
            }
            if (merchant.status == "1")
            {
                swstatus.Checked = true;
            }
            else
            {
                swstatus.Checked = false;
            }
        }

        private void txtminiute_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    Convert.ToInt32(txtminiute.Text);
            //}
            //catch (Exception)
            //{
            //    txtminiute.Text = "00";
            //    MessageBox.Show("请正常设置时间");
            //}
        }

        private void txtminiute1_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    Convert.ToInt32(txtminiute1.Text);
            //}
            //catch (Exception)
            //{
            //    txtminiute1.Text = "00";
            //    MessageBox.Show("请正常设置时间");
            //}
        }

        private void txthour_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txthour.Text) < 10 && Convert.ToInt32(txthour.Text) > 0)
                {
                    int hour = Convert.ToInt32(txthour.Text);
                    txthour.Text = "0" + hour;
                }
            }
            catch (Exception)
            {

                txthour.Text = "00";
            }
        }

        private void txtminiute_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txtminiute.Text) < 10 && Convert.ToInt32(txtminiute.Text) > 0)
                {
                    int miniute = Convert.ToInt32(txtminiute.Text);
                    txtminiute.Text = "0" + miniute;
                }
            }
            catch (Exception)
            {

                txtminiute.Text = "00";
            }
        }

        private void txthour1_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txthour1.Text) < 10 && Convert.ToInt32(txthour1.Text) > 0)
                {
                    int hour = Convert.ToInt32(txthour1.Text);
                    txthour1.Text = "0" + hour;
                }
            }
            catch (Exception)
            {

                txthour1.Text = "00";
            }
        }

        private void txtminiute1_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txtminiute1.Text) < 10 && Convert.ToInt32(txtminiute1.Text) > 0)
                {
                    int miniute = Convert.ToInt32(txtminiute1.Text);
                    txthour.Text = "0" + miniute;
                }
            }
            catch (Exception)
            {

                txtminiute1.Text = "00";
            }
        }

        private void swstatus_CheckedChanged(object sender, EventArgs e)
        {
            if (swstatus.Checked == true)
                merchantDal.Updatestatus("1", merchantid);
            else
                merchantDal.Updatestatus("2", merchantid);
        }
    }
}